//
//  SSPPublishContentCell.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/10/27.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "SSPPublishContentCell.h"

#import <PYPhotoBrowser/PYPhotoBrowser.h>

@interface SSPPublishContentCell()

/// viewModle
@property (nonatomic, readwrite, strong) SSPPublishContentItemViewModel *viewModel;

/// IBOutlet
@property (weak, nonatomic) IBOutlet UILabel* nameLabel;
@property (weak, nonatomic) IBOutlet UILabel* timeLabel;
@property (weak, nonatomic) IBOutlet UIView* tagView;
@property (weak, nonatomic) IBOutlet UILabel* contentLabel;
@property (weak, nonatomic) IBOutlet UILabel* examineLabel;
@property (weak, nonatomic) IBOutlet UILabel* cityLabel;
@property (weak, nonatomic) IBOutlet UIButton* zfBtn;
@property (weak, nonatomic) IBOutlet UIButton* zanBtn;
@property (weak, nonatomic) IBOutlet UIButton* commentBtn;
@property (weak, nonatomic) IBOutlet UIButton* avatarImgBtn;
@property (weak, nonatomic) IBOutlet UIView* imagesView;
@property (weak, nonatomic) IBOutlet UIImageView* avatarImgView;
@property (weak, nonatomic) IBOutlet UIView* funcView;

@end

@implementation SSPPublishContentCell

- (void)awakeFromNib
{
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

#pragma mark - bind data
- (void)bindViewModel:(SSPPublishContentItemViewModel *)viewModel
{
    @weakify(self);
    
    self.viewModel = viewModel;
    
    DataManager* dm = [DataManager sharedDataManager];
    
    NSString* areaID = @"";
    NSString* nickName = @"";
    NSString* addTime = @"";
    NSString* content = @"";
    NSString* examine = @"";
    NSInteger transmitNum = 0;
    NSInteger praiseNum = 0;
    NSInteger commentNum = 0;
    NSString* headUrl = @"";
    NSArray* images = @[];
    
    if ([viewModel.data isKindOfClass:[SSPListData class]])
    {
        SSPListData* listData = viewModel.data;
        
        areaID = listData.areaID;
        nickName = listData.user.wxNickName;
        addTime = listData.addTime;
        content = listData.content;
        transmitNum = listData.transmit;
        praiseNum = listData.praise;
        commentNum = listData.comment;
        headUrl = listData.user.wxHeadImg;
        images = listData.images;
        
        NSInteger status = listData.status;
        examine = listData.isMyself ? (status == 1 ? @"已发布" : (status == 2 ? @"下架" : @"待审核")) : @"";
    }
    else if ([viewModel.data isKindOfClass:[SSPDetailData class]])
    {
        SSPDetailData* detailData = viewModel.data;
        
        areaID = detailData.areaID;
        nickName = detailData.user.wxNickName;
        headUrl = detailData.user.wxHeadImg;
        addTime = detailData.addTime;
        content = detailData.content;
        transmitNum = detailData.transmitCount;
        praiseNum = detailData.praiseCount;
        commentNum = detailData.commentCount;
        images = detailData.images;
    }
    
    NSString* strTransmitNum = HCStrFormat(@"%d", (int)transmitNum);
    strTransmitNum = @"";
    NSString* strPraiseNum = HCStrFormat(@"%d", (int)praiseNum);
    NSString* strCommentNum = HCStrFormat(@"%d", (int)commentNum);
    
    self.nameLabel.text = nickName;
    self.timeLabel.text = addTime;
    self.contentLabel.text = content;
    {
        if (!viewModel.isShowDetail)
        {
            [self.contentLabel mas_remakeConstraints:^(MASConstraintMaker *make)
            {
                @strongify(self);
                
                make.height.mas_lessThanOrEqualTo(60);
            }];
        }
    }
    self.examineLabel.text = examine;
    self.cityLabel.text = @"全国";
    {
        if ([areaID isNotBlank])
        {
            ProvinceListData* findData = nil;
            for (ProvinceListData* curPPListData in dm.addrData.datas)
            {
                for (ProvinceListData* curSPListData in curPPListData.datas)
                {
                    if ([curSPListData.areaID isEqualToString:areaID])
                    {
                        findData = curSPListData;
                        
                        break;
                    }
                }
                
                if (findData)
                {
                    self.cityLabel.text = findData.name;
                    
                    break;
                }
            }
        }
    }
//    self.zfBtn.titleLabel.text = strTransmitNum;
//    [self.zfBtn setTitle:strTransmitNum forState:UIControlStateNormal];
//    self.zanBtn.titleLabel.text = strPraiseNum;
    [self.zanBtn setTitle:strPraiseNum forState:UIControlStateNormal];
//    self.commentBtn.titleLabel.text = strCommentNum;
    [self.commentBtn setTitle:strCommentNum forState:UIControlStateNormal];
    
    self.avatarImgBtn.rac_command = self.viewModel.avatarCommand;
    
    BOOL isHaveImages = images.count > 0;
    if (isHaveImages)
    {
        [self.imagesView removeAllSubviews];
        
        NSInteger imgColNum = 3;
        
        // 计算大小
        CGFloat imgOffset = 16;
        CGFloat imgSize = (self.imagesView.frame.size.width - ((imgColNum + 1)*imgOffset)) / imgColNum;
        
        PYPhotosView *photosView = [PYPhotosView photosViewWithImages:[NSMutableArray array]];
        photosView.photosState = PYPhotosViewStateDidCompose;
        photosView.photosMaxCol = imgColNum;
        photosView.oneImageFullFrame = NO;
        photosView.autoLayoutWithWeChatSytle = YES;
        photosView.autoSetPhotoState = NO;
        photosView.oneImageFullFrame = NO;
        photosView.photoWidth = imgSize;
        photosView.photoHeight = imgSize;
        photosView.photoMargin = imgOffset;
        
        NSInteger row = 1;
        photosView.frame = (CGRect) {0, 0, self.imagesView.frame.size.width, imgSize*row + imgOffset*(row - 1)};
        photosView.tag = 12;
        [self.imagesView addSubview:photosView];
        
        photosView.originalUrls = images;
        
        //修改约束
        for (NSLayoutConstraint* curConstraint in self.imagesView.constraints)
        {
            if (curConstraint.firstAttribute == NSLayoutAttributeHeight)
            {
                curConstraint.constant = photosView.frame.size.height;
            }
            else if (curConstraint.firstAttribute == NSLayoutAttributeTop)
            {
                curConstraint.constant = 12;
            }
        }
    }
    else
    {
        for (NSLayoutConstraint* curConstraint in self.imagesView.constraints)
        {
            if (curConstraint.firstAttribute == NSLayoutAttributeHeight)
            {
                curConstraint.constant = 0;
            }
            else if (curConstraint.firstAttribute == NSLayoutAttributeTop)
            {
                curConstraint.constant = 0;
            }
        }
    }
    
    for (NSLayoutConstraint* curConstraint in self.funcView.constraints)
    {
        if (curConstraint.firstAttribute == NSLayoutAttributeHeight)
        {
            curConstraint.constant = self.viewModel.isShowTags ? 40 : 0;
            break;
        }
    }
    
    self.tagView.hidden = NO;
    self.tagView.layer.cornerRadius = self.tagView.frame.size.height/2.0f;
    self.tagView.layer.masksToBounds = YES;
    
    self.avatarImgView.layer.cornerRadius = self.avatarImgView.frame.size.height/2.0f;
    self.avatarImgView.layer.masksToBounds = YES;
    [self.avatarImgView yy_setImageWithURL:[NSURL URLWithString:headUrl] placeholder:MHImageNamed(@"placeholderLogo")];
}

@end
